const Stock = require("../modules/Stock")

module.exports = async ({ code, type }) => {
    let result = {}
    let stock = new Stock({ code, type })

    let realtime = await stock.getRealtime()
    let { list, maxPoints } = realtime
    let transaction = await stock.getTransaction({ ktype: "min1" })
    let details = await stock.generateDetails({ transaction })
    let detailsLatest = Stock.getDetailsLatest({ details, latest: list.length })

    let divergence = Stock.divergence({ details: detailsLatest })
    
    detailsLatest.analysis = {
        divergence
    }

    delete detailsLatest.transaction
    delete detailsLatest.calculate.macd.dea
    delete detailsLatest.calculate.macd.diff
    delete detailsLatest.calculate.kd.d

    detailsLatest.calculate.macd.macd.length = maxPoints
    detailsLatest.calculate.kd.k.length = maxPoints

    result = { ...realtime, details: detailsLatest }

    return result
}